package com.ljh.SimpleTradingSystem.controller;

import com.ljh.SimpleTradingSystem.mybatis.entity.User;
import com.ljh.SimpleTradingSystem.mybatis.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class RegisterController {
    @Resource
    UserMapper userMapper;

    @GetMapping("/register")
    public String registerForm() {
        return "Register";
    }

    @PostMapping("/register")
    @ResponseBody
    public String register(@RequestParam String userName, @RequestParam String password) {
        User user = userMapper.selectByName(userName);
        if (user == null) {
            userMapper.insert(new User(userName, password, 0, "null"));
            return "注册成功";
        } else {
            return "该用户名已经存在";
        }
    }
}
